import { defineConfig, loadEnv } from 'vite'
import type { ConfigEnv, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

// vs code 编辑器配置
import monacoEditorPlugin from 'vite-plugin-monaco-editor'

// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd());

  return defineConfig({
    plugins: [
      vue(),
      (monacoEditorPlugin as any).default({
        languageWorkers: ['editorWorkerService', 'typescript', 'json', 'html']
      })
    ],
    resolve: {
      alias: {
        '@': path.join(__dirname, 'src'),
        'generator': path.join(__dirname, 'src/components/AppGenerator')
      }
    },
    server: {
      port: 3000,
      host: '0.0.0.0',
      open: false,
      // base: "./ ", //生产环境路径
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_APP_SERVER_URL,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    }
  })
}
